from PIL import Image
 
def merge_images(image1_path, image2_path, output_path):
    # 打开图片
    image1 = Image.open(image1_path)
    image2 = Image.open(image2_path)
 
    # 获取图片的宽度和高度
    width1, height1 = image1.size
    width2, height2 = image2.size
 
    # 创建一个新图片用于合并
    new_image = Image.new('RGB', (width1, height1 + height2))
 
    # 将第一张图片粘贴到新图片上
    new_image.paste(image1, (0, 0))
    # 将第二张图片粘贴到新图片上，紧接着第一张图片
    new_image.paste(image2, (0, height1))
 
    # 保存新图片
    new_image.save(output_path)
 
# 使用函数合并图片
merge_images('path_to_image1.jpg', 'path_to_image2.jpg', 'merged_image.jpg')
